﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using LLoger;
using LimitatorInterfaces;
using LimitatorInterfaces.UnloadServices;
using TRANS2QUIKSHARP;

namespace UnloadServices
{
   /* /// <summary>
    /// Выгрузка лимитов в фортс через 
    /// </summary>
    public class DirectFortsLimitUnloadService : UnloadServiceBase
    {
        public override void UnloadLimits()
        {
            try
            {
                if (SourceLimits.Count == 0)
                    throw new UnloadLimitsException("Список лимитов пуст");

                var qRes = QUIKAPI.Connect((string)GetSettingsValue("QuikPath"));

                if (qRes != eQUIKResult.SUCCESS)
                    throw new UnloadLimitsException("Ошибка при подключении к терминалу КВИК");

                int tranId = 0;
                SourceLimits.ForEach(l =>
                    {
                        tranId++;
                        string tran = MakeLimitTransaction(tranId, l.ClientCode, l.Value);
                        SendTransaction(tran);
                    });

            }
            catch (Exception ex)
            {
                LogWriter.WriteLine(ex);
                throw;
            }
            finally
            {
                if (QUIKAPI.TestConnectionDLL() == eQUIKResult.DLL_CONNECTED)
                    QUIKAPI.Disconnect();
            }
        }

        private void SendTransaction(string tran)
        {
            var res = QUIKAPI.SendSyncTransaction(tran, 0.0);
            if (res != eQUIKResult.SUCCESS)
                LogWriter.WriteLine("Транзакция {0} не выполнена. Код: {1}", tran, res.ToString());
        }

        private string MakeLimitTransaction(int transId, string clientCode, decimal value)
        {
            string tran = (string)GetSettingsValue("SetLimitTmpl");

            if (string.IsNullOrEmpty(tran))
                throw new UnloadLimitsException("Unable to find 'SetLimitTmpl' setting");

            return string.Format(tran, transId, clientCode, value.ToString("#.00"));
        }

        public DirectFortsLimitUnloadService(string configPath, ILogWriter logWriter)
            : base(configPath, logWriter)
        {
        }
    }*/
}
